knitr::opts_chunk$set(echo = TRUE)
getwd()
spruce.df = read.csv("SPRUCE.csv") head(spruce.df)
with(spruce.df,plot(Height~BHDiameter,bg="Blue",pch=21,cex=1.2,xlim=c(0, 1.1*max(BHDiameter)),ylim=c(0, 1.1*max(Height))))
library(s20x) layout(matrix(1:3,nr=3,nc=1,byrow=T,)) trendscatter(Height~BHDiameter,f=0.5, data=spruce.df, main = "Plot of Height vs Diameter (lowess+/-sd) f=0.5") trendscatter(Height~BHDiameter,f=0.6, data=spruce.df, main = "Plot of Height vs Diameter (lowess+/-sd) f=0.6") trendscatter(Height~BHDiameter,f=0.7, data=spruce.df, main = "Plot of Height vs Diameter (lowess+/-sd) f=0.7")
spruce.lm <- lm(Height~BHDiameter, data = spruce.df) plot(Height~BHDiameter, data = spruce.df) abline(spruce.lm)
layout(matrix(1:4,nr=2,nc=2,byrow=TRUE)) ### First graph with(spruce.df, plot(Height~BHDiameter,bg="Blue",pch=21,ylim=c(0,1.1*max(Height)),xlim=c(0,1.1*max(BHDiameter))) ) abline(spruce.lm) ### Second graph with(spruce.df, plot(Height~BHDiameter,bg="Blue",pch=21,ylim=c(0,1.1*max(Height)),xlim=c(0,1.1*max(BHDiameter))) ) abline(spruce.lm) ### Third graph yhat=fitted(spruce.lm) with(spruce.df,{ segments(BHDiameter,Height,BHDiameter,yhat) }) with(spruce.df, plot(Height~BHDiameter,bg="Blue",pch=21,ylim=c(0,1.1*max(Height)),xlim=c(0,1.1*max(BHDiameter))) ) #make nieve model with(spruce.df, abline(h=mean(Height))) abline(spruce.lm) #make the explained deviations (explained by the model) with(spruce.df, segments(BHDiameter,mean(Height),BHDiameter,yhat,col="Red")) ### Fourth graph with(spruce.df, plot(Height~BHDiameter,bg="Blue",pch=21,ylim=c(0,1.1*max(Height)),xlim=c(0,1.1*max(BHDiameter))) ) with(spruce.df,abline(h=mean(Height))) with(spruce.df, segments(BHDiameter,Height,BHDiameter,mean(Height),col="Green"))
RSS=with(spruce.df,sum((Height-yhat)^2)) MSS=with(spruce.df,sum((yhat-mean(Height))^2)) TSS=with(spruce.df,sum((Height-mean(Height))^2)) print(paste0("TSS: ", TSS)) print(paste0("MSS: ", MSS)) print(paste0("RSS: ", RSS)) print(paste0("MSS/TSS: ", MSS/TSS)) print(paste0("TSS = MSS+RSS?: ", MSS + RSS, " ... Yes."))
summary(spruce.lm)
0.4815
9.1468
HEIGHT = .4815*BHDiameter + 9.1468
predict(spruce.lm)
Prediction when diameter is 15cm: 18.7763
Prediction when diameter is 18cm: 11.9394
Prediction when diameter is 20cm: 19.1134
library(ggplot2) g=ggplot(spruce.df, aes(x=BHDiameter,y=Height,colour=BHDiameter)) g=g+geom_point() + geom_line()+ geom_smooth(method="lm") g+ggtitle("Height Vs BHDiameter")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.